package com.atguigu.tingshu.album.task;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.service.AlbumInfoService;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.user.client.UserClientFeign;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;


/***
 * 定时任务，将专辑数据写进Redis
 */
@Component
public class AlbumFromMysqlToRedisTask {

    private static final Long SIZE = 1000L;
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private AlbumInfoService albumInfoService;

    @Autowired
    private UserClientFeign userClientFeign;

    @Autowired
    private BaseCategoryService baseCategoryService;


    @Scheduled(cron = "0 0 3 * * ?")
    public void sdfa() {
        //写专辑数据
        //获取所有数据
        long count = albumInfoService.count();
        //分页，一页多少条数据？ ---》一条数据大小决定
        //页数。根据总数据量/1000
        var size = count % SIZE == 0 ? count / SIZE : count / SIZE + 1;

        HashSet<String> set = new HashSet();

        //循环遍历
        for (int i = 1; i <= size; i++) {
            //获取分页的数据。根据每页1000条数据分页
            List<AlbumInfo> records = albumInfoService.page(new Page<>(i, SIZE)).getRecords();
            //将分页的所有数据 写到Redis
            records.stream().forEach(albumInfo -> {
//                Boolean lock = redisTemplate.opsForValue().setIfAbsent("lock", "aaa");
//                if (lock) {

                String key = "getAlbumInfo:" + Arrays.asList(albumInfo.getId()).toString();
                set.add(key);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {

                }

                redisTemplate.opsForValue().set(key,
                        JSONObject.toJSONString(albumInfo),
                        1, TimeUnit.DAYS);

                //写专辑统计数据
                Map<String, Object> albumStatMap = albumInfoService.getAlbumStatMap(albumInfo.getId());
                redisTemplate.opsForValue().set("getAlbumInfoStat:" + Arrays.asList(albumInfo.getId()).toString(),
                        JSONObject.toJSONString(albumStatMap),
                        1, TimeUnit.DAYS);
                //写作者数据
                userClientFeign.getUserInfo(albumInfo.getUserId());

                    redisTemplate.delete("lock");
//                }
            });
        }
    }

    /**
     * 定时任务，分类数据每个月1号凌晨4点执行
     */
    @Scheduled(cron = "0 0 4 1 * * ")
    public void categoryToRedis() {
        //写分类数据
        List<BaseCategoryView> list = baseCategoryService.list();
        list.stream().forEach(baseCategoryView -> {

            String jsonString = JSONObject.toJSONString(baseCategoryView);

            redisTemplate.opsForValue().set("getBaseCategoryView:" + Arrays.asList(baseCategoryView.getCategory3Id()).toString(),
                    jsonString,
                    30, TimeUnit.DAYS);
        });
    }
}
